package com.paperless.sdk;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ResourceUtils;

import org.ini4j.Config;
import org.ini4j.Ini;

import java.io.File;
import java.io.IOException;

/**
 * @author Created by xlk on 2021/9/26.
 */
public class IniUtil {

    private final String TAG = "IniUtil-->";
    private static IniUtil instance;
    private final Ini ini = new Ini();
    public boolean isLoaded;//加载ini文件成功则为true

    private IniUtil() {
        Config config = new Config();
        //不允许出现重复的部分和选项
        config.setMultiSection(false);
        config.setMultiOption(false);
        ini.setConfig(config);
    }

    public static IniUtil getInstance() {
        if (instance == null) {
            synchronized (IniUtil.class) {
                if (instance == null) {
                    instance = new IniUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 加载ini文件
     *
     * @param filePath ini文件路径
     */
    public boolean loadFile(String filePath) {
        return loadFile(new File(filePath));
    }

    public boolean loadFile(File file) {
        if (isLoaded) return true;
        try {
            ini.setFile(file);
            ini.load();
            isLoaded = true;
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 检查ini文件配置参数，并在没有配置的时候设置默认值
     * @param sectionName
     * @param optionName
     * @param defaultValue 默认值
     */
    public void checkParams(String sectionName, String optionName, Object defaultValue) {
        String value = get(sectionName, optionName);
        if (value.isEmpty()) {
            put(sectionName, optionName, defaultValue);
        }
        LogUtils.i("checkParams ["+sectionName+"],"+optionName+"="+value+",defaultValue:"+defaultValue);
    }

    public String get(String sectionName, String optionName) {
        if (!isLoaded) {
            throw new IllegalStateException("ini文件还未加载成功！");
        }
        String s = ini.get(sectionName, optionName);
        if (s == null) {
            s = "";
        }
        return s;
    }

    public void put(String sectionName, String optionName, Object value) {
        if (!isLoaded) {
            throw new IllegalStateException("ini文件还未加载成功！");
        }
        ini.put(sectionName, optionName, value);
    }

    public boolean store() {
        if (!isLoaded) {
            throw new IllegalStateException("ini文件还未加载成功！");
        }
        try {
            ini.store();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.e(e);
        }
        return false;
    }

    public void defaultInitConfigFile(String rootPath) {
        String iniPath = rootPath + File.separator + "client.ini";
        String devPath = rootPath + File.separator + "client.dev";
        if (!FileUtils.isFileExists(iniPath)) {
            ResourceUtils.copyFileFromAssets("client.ini", iniPath);
        }
        if (loadFile(iniPath)) {
            put("Buffer Dir", "configdir", rootPath);
            put("Buffer Dir", "mediadir", rootPath + "mediadir" + File.separator);
            store();
        }
        FileUtils.delete(devPath);
        ResourceUtils.copyFileFromAssets("client.dev", devPath);
    }

    //<editor-fold desc="selfinfo">

    public boolean isTcpMode() {
        String result = ini.get("selfinfo", "streamprotol");
        return "1".equals(result);
    }

    public void switchTcpMode(boolean enable) {
        ini.put("selfinfo", "streamprotol", enable ? "1" : "0");
    }

    public String getHardver() {
        String result = ini.get("selfinfo", "hardver");
        return result == null ? "" : result;
    }

    public void setHardver(String hardver) {
        ini.put("selfinfo", "hardver", hardver);
    }

    public String getSoftver() {
        String result = ini.get("selfinfo", "softver");
        return result == null ? "" : result;
    }

    public void setSoftver(String softver) {
        ini.put("selfinfo", "softver", softver);
    }

    public String getDate() {
        String result = ini.get("selfinfo", "date");
        return result == null ? "" : result;
    }

    public void setDate(String date) {
        ini.put("selfinfo", "date", date);
    }

    /**
     * 获取丢帧间隔
     *
     * @return 单位毫秒
     */
    public String getDiscardMs() {
        String result = ini.get("selfinfo", "discardms");
        return result == null ? "" : result;
    }

    /**
     * 设置丢帧间隔
     *
     * @param ms 毫秒
     */
    public void setDiscardMs(String ms) {
        ini.put("selfinfo", "discardms", ms);
    }

    /**
     * 获取视频播放缓冲
     *
     * @return 单位毫秒
     */
    public String getPlayCacheMs() {
        String result = ini.get("selfinfo", "playcachems");
        return result == null ? "" : result;
    }

    /**
     * 设置丢帧间隔
     *
     * @param ms 毫秒
     */
    public void setPlayCacheMs(String ms) {
        ini.put("selfinfo", "playcachems", ms);
    }

    /**
     * 是否已开启同屏调试
     */
    public boolean isSameScreenDebug() {
        String result = ini.get("selfinfo", "capturedebug");
        return "1".equals(result);
    }

    /**
     * 切换同屏调试开关
     */
    public void switchSameScreenDebug(boolean enable) {
        ini.put("selfinfo", "capturedebug", enable ? "1" : "0");
    }

    /**
     * 获取网络调试状态
     */
    public boolean isTickDebug() {
        String result = ini.get("selfinfo", "tickdebug");
        return "1".equals(result);
    }

    /**
     * 切换同屏调试开关
     */
    public void switchTickDebug(boolean enable) {
        ini.put("selfinfo", "tickdebug", enable ? "1" : "0");
    }

    //</editor-fold>

    //<editor-fold desc="tick">

    /**
     * 获取断线重连间隔
     *
     * @return 单位毫秒
     */
    public String getReconnectInterval() {
        String result = ini.get("tick", "int");
        return result == null ? "" : result;
    }

    /**
     * 设置短线重连间隔
     *
     * @param ms 单位毫秒
     */
    public void setReconnectInterval(String ms) {
        ini.put("tick", "int", ms);
    }

    /**
     * 重试次数
     *
     * @return
     */
    public String getOfftryTimes() {
        String result = ini.get("tick", "offtrytimes");
        return result == null ? "" : result;
    }

    public void setOfftryTimes(String times) {
        ini.put("tick", "offtrytimes", times);
    }

    //</editor-fold>

    //<editor-fold desc="Buffer Dir">

    public String getConfigDir() {
        String result = ini.get("Buffer Dir", "configdir");
        return result == null ? "" : result;
    }

    public void setConfigDir(String configDir) {
        ini.put("Buffer Dir", "configdir", configDir);
    }

    public String getMediaDir() {
        String result = ini.get("Buffer Dir", "mediadir");
        return result == null ? "" : result;
    }

    public void setMediaDir(String mediaDir) {
        ini.put("Buffer Dir", "mediadir", mediaDir);
    }

    public String getMediaDirSize() {
        String result = ini.get("Buffer Dir", "mediadirsize");
        return result == null ? "" : result;
    }

    public void setMediaDirSize(String mediaDirSize) {
        ini.put("Buffer Dir", "mediadirsize", mediaDirSize);
    }

    //</editor-fold>

    //<editor-fold desc="areaaddr">

    public void setIp(String ip) {
        ini.put("areaaddr", "area0ip", ip);
    }

    public String getIp() {
        String result = ini.get("areaaddr", "area0ip");
        return result == null ? "" : result;
    }

    public void setPort(String port) {
        ini.put("areaaddr", "area0port", port);
    }

    public String getPort() {
        String result = ini.get("areaaddr", "area0port");
        return result == null ? "" : result;
    }

    //</editor-fold>

    //<editor-fold desc="nosdl">

    /**
     * 是否编码过滤
     */
    public boolean isEncodeFilter() {
        String result = ini.get("nosdl", "disablebsf");
        return "0".equals(result);
    }

    public void switchEncodeFilter(boolean enable) {
        ini.put("nosdl", "disablebsf", enable ? "0" : "1");
    }

    //</editor-fold>

    //<editor-fold desc="other">

    public String getBitRate() {
        String result = ini.get("other", "maxBitRate");
        return result == null ? "" : result;
    }

    public void setBitRate(String bitrate) {
        ini.put("other", "maxBitRate", bitrate);
    }

    /**
     * 获取屏幕录制帧率
     *
     * @return 每秒多少帧发送出去
     */
    public String getFrameRate() {
        String result = ini.get("other", "framerate");
        return result == null ? "" : result;
    }

    /**
     * 获取屏幕录制帧率
     *
     * @return 每秒多少帧发送出去
     */
    public void setFrameRate(String frameRate) {
        ini.put("other", "framerate", frameRate);
    }

    /**
     * 获取关键帧间隔
     *
     * @return 单位秒
     */
    public String getIFrameInterval() {
        String result = ini.get("other", "i_frame_interval");
        return result == null ? "" : result;
    }

    /**
     * 获取关键帧间隔
     *
     * @return 单位秒
     */
    public void setIFrameInterval(String i_frame_interval) {
        ini.put("other", "i_frame_interval", i_frame_interval);
    }

    //</editor-fold>

    //<editor-fold desc="debug">
    public boolean isAgenda3Mode() {
        String result = ini.get("debug", "agendav3");
        return "1".equals(result);
    }

    public void switchAgenda3mode(boolean value) {
        ini.put("debug", "agendav3", value ? "1" : "0");//议题模式3
    }

    public boolean isMutilOper() {
        String result = ini.get("debug", "enablemutiloper");
        return "1".equals(result);
    }

    public void switchMultiOper(boolean value) {
        ini.put("debug", "enablemutiloper", value ? "1" : "0");//批量操作
    }

    /**
     * 获取是否短线重连
     */
    public String getKeyBind() {
        String result = ini.get("debug", "keybind");
        return result == null ? "" : result;
    }

    /**
     * 获取是否短线重连
     */
    public boolean isDisconnectReconnect() {
        String result = ini.get("debug", "streamreconnect");
        return "1".equals(result);
    }

    /**
     * 切换短线重连开关
     */
    public void switchDisconnectReconnect(boolean enable) {
        ini.put("debug", "streamreconnect", enable ? "1" : "0");
    }

    /**
     * 获取当前是否打开调试开关
     */
    public boolean isDebug() {
        String result = ini.get("debug", "enabledebug");
        return "1".equals(result);
    }

    /**
     * 切换打开调试开关
     */
    public void switchDebug(boolean enable) {
        ini.put("debug", "enabledebug", enable ? "1" : "0");
    }

    /**
     * 是否为新界面状态
     */
    public boolean isNewFacePost() {
        String result = ini.get("debug", "enablenewfacepost");
        return "1".equals(result);
    }

    /**
     * 切换新界面状态开关
     */
    public void switchNewFacePost(boolean enable) {
        ini.put("debug", "enablenewfacepost", enable ? "1" : "0");
    }

    /**
     * 是否启动桌面采集声卡回放 0关闭 1开启 启用将扬声器输出捕获并附加到2号视频通道 当videoaudio=1启用时 注:优先使用输入音频附加
     * 是否启动桌面采集声卡回放 0关闭禁用 1开启禁用 启用将扬声器输出捕获并附加到2号视频通道
     * 当videoaudio=1启用时 注:优先使用输入音频附加
     */
    public boolean isSameScreenSound() {
        String result = ini.get("debug", "disableaudio");
        return "0".equals(result);
    }

    /**
     * 切换同屏声音开关
     */
    public void switchSameScreenSound(boolean enable) {
        ini.put("debug", "disableaudio", enable ? "0" : "1");
    }

    /**
     * 将音频附加到视频通道中 0表示关闭 1表示开启
     * @return
     */
    public boolean isSameScreenMic() {
        String result = ini.get("debug", "videoaudio");
        return "1".equals(result);
    }

    /**
     * 切换同屏麦克风开关
     */
    public void switchSameScreenMic(boolean enable) {
        ini.put("debug", "videoaudio", enable ? "1" : "0");
    }

    public String getNotifyMs() {
        String result = ini.get("debug", "notifyms");
        return result == null ? "" : result;
    }

    public void setNotifyMs(String ms) {
        ini.put("debug", "notifyms", ms);
    }

    /**
     * 视频播放是否为单个屏
     *
     * @return false 表示使用4分屏
     */
    public boolean isSingleWindow() {
        String result = ini.get("debug", "streammode");
        return "1".equals(result);
    }

    /**
     * 视频播放窗口单屏和4分屏模式切换
     */
    public void switchSingleWindow(boolean enable) {
        ini.put("debug", "streammode", enable ? "1" : "2");
    }

    public boolean isEncryption() {
        String result = ini.get("debug", "protolencrypt");
        return "1".equals(result);
    }

    public void switchEncryption(boolean enable) {
        ini.put("debug", "protolencrypt", enable ? "1" : "0");
    }

    public boolean isDisableMulticast() {
        String result = ini.get("debug", "disablemulticast");
        return "1".equals(result);
    }

    public void switchDisableMulticast(boolean enable) {
        ini.put("debug", "disablemulticast", enable ? "1" : "0");
    }

    // PC 相关配置

    /**
     * 是否开启显卡硬编码
     */
    public boolean isHwEncode() {
        String result = ini.get("debug", "hwencode");
        return "1".equals(result);
    }

    public void switchHwEncode(boolean enable) {
        ini.put("debug", "hwencode", enable ? "1" : "0");
    }

    /**
     * 是否开启显卡硬解码
     */
    public boolean isHwDecode() {
        String result = ini.get("debug", "hwdecode");
        return "1".equals(result);
    }

    public void switchHwDecode(boolean enable) {
        ini.put("debug", "hwdecode", enable ? "1" : "0");
    }

    /**
     * 是否开启调试窗口
     */
    public boolean isOpenDebugWindow() {
        String result = ini.get("debug", "console");
        return "1".equals(result);
    }

    public void switchOpenDebugWindow(boolean enable) {
        ini.put("debug", "console", enable ? "1" : "0");
    }

    /**
     * 是否开启USB视频设备采集
     */
    public boolean isOpenUsbCap() {
        String result = ini.get("debug", "camaracap");
        return "1".equals(result);
    }

    public void switchOpenUsbCap(boolean enable) {
        ini.put("debug", "camaracap", enable ? "1" : "0");
    }

    /**
     * 上传超高清视频时转换流畅格式
     */
    public boolean isMediaTranscode() {
        String result = ini.get("debug", "mediatranscode");
        return "1".equals(result);
    }

    public void switchMediaTranscode(boolean enable) {
        ini.put("debug", "mediatranscode", enable ? "1" : "0");
    }

    /**
     * 设置流编码模式0高质量
     *
     * @return 1中等 2低带宽 默认0
     */
    public int getEncodeMode() {
        String encmode = ini.get("debug", "encmode");
        try {
            return Integer.parseInt(encmode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getVideoStream2MaxWidth() {
        String stream3width = ini.get("debug", "stream2width");
        return stream3width == null ? "" : stream3width;
    }

    public String getVideoStream2MaxHeight() {
        String stream3height = ini.get("debug", "stream2height");
        return stream3height == null ? "" : stream3height;
    }

    public String getVideoStream3MaxWidth() {
        String stream3width = ini.get("debug", "stream3width");
        return stream3width == null ? "" : stream3width;
    }

    public String getVideoStream3MaxHeight() {
        String stream3height = ini.get("debug", "stream3height");
        return stream3height == null ? "" : stream3height;
    }

    //</editor-fold>

    //<editor-fold desc="tablecard">

    /**
     * 桌牌是否自动缩放字体大小
     */
    public boolean isTableCardUseBackendFontSize() {
        String result = ini.get("tablecard", "useBackendFontSize");
        return "1".equals(result);
    }

    /**
     * 切换字体大小自动缩放开关
     */
    public void switchTableCardUseBackendFontSize(boolean value) {
        ini.put("tablecard", "useBackendFontSize", value ? "1" : "0");
    }

    /**
     * 桌牌内容垂直居中是否开启
     */
    public boolean isTableCardContentVerticalCenter() {
        String result = ini.get("tablecard", "contentVerticalCenter");
        return "1".equals(result);
    }

    /**
     * 切换桌牌内容垂直居中开关
     */
    public void switchTableCardContentVerticalCenter(boolean value) {
        ini.put("tablecard", "contentVerticalCenter", value ? "1" : "0");
    }

    /**
     * view是否自动缩放
     */
    public boolean isTableCardAutoScaleView() {
        String result = ini.get("tablecard", "autoScaleView");
        return "1".equals(result);
    }

    /**
     * view自动缩放开关
     */
    public void switchTableCardAutoScaleView(boolean value) {
        ini.put("tablecard", "autoScaleView", value ? "1" : "0");
    }

    //</editor-fold>
}
